﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MallInteractiveMap.CMSWebClient.Common;
using MallInteractiveMap.CMSWebClient.Presenter;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;

namespace MallInteractiveMap.CMSWebClient.BasicView
{
    public partial class FloorDetail : DetailBasePage<DTOFloor>
    {
        private string LISTDDATA = "LISTDATAFloorDetail";
        private FloorPresenter _presenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new FloorPresenter();
            tblFloor.Edit_Click += new EventHandler(tblMall_Edit_Click);
            tblFloor.Delete_Click += new EventHandler(tblMall_Delete_Click);
            if (!IsPostBack)
            {
                PrepareView();
            }
        }

        void tblMall_Delete_Click(object sender, EventArgs e)
        {
            if (FloorGrid.SelectedIndex < 0) return;
            if (_presenter.Delete(Convert.ToInt32(FloorGrid.SelectedValue)))
            {
                ShowMessage(LanguageLoader.GetText("FLOOR_DELETE_SUCCESS"), LanguageLoader.GetText("SUCCESS"));
                PrepareView();
            }
        }

        void tblMall_Edit_Click(object sender, EventArgs e)
        {
            if (FloorGrid.SelectedIndex < 0) return;
            Response.Redirect(string.Format("~/BasicView/Floor.aspx?Id={0}", FloorGrid.SelectedValue.ToString()));
        }

        private void PrepareView()
        {
            Session[this.LISTDDATA] = _presenter.GetFloors();
            BindGrid();
        }


        /// <summary>
        /// Bind the data grid.
        /// </summary>
        private void BindGrid()
        {
            if (Session[this.LISTDDATA] == null)
            {
                return;
            }
            this.ViewData = Session[this.LISTDDATA] as List<DTOFloor>;
            FloorGrid.DataSource = this.ViewData;
            FloorGrid.DataBind();
        }

        #region Grid related Events
        public void FloorGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (Session[this.LISTDDATA] != null)
            {
                base.ViewData = Session[this.LISTDDATA] as List<DTOFloor>;
                Session[this.LISTDDATA] = SortViewData(e.SortExpression);
                BindGrid();
            }
        }

        protected void FloorGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            FloorGrid.PageIndex = e.NewPageIndex;
            BindGrid();
            FloorGrid.SelectedIndex = -1;
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            SetLanguageAndResource();
        }

        private void SetLanguageAndResource()
        {
            FloorGrid.EmptyDataText = LanguageLoader.GetText("NO_AVAILABLE_FLOOR");
            FloorGrid.Columns[0].HeaderText = LanguageLoader.GetText("NAME");
            FloorGrid.Columns[1].HeaderText = LanguageLoader.GetText("FLOOR_INDEX");
            FloorGrid.Columns[2].HeaderText = LanguageLoader.GetText("MAP_TITLE");
        }
    }
}